package com.twu.biblioteca.controller;

import com.twu.biblioteca.BibliotecaApp;

import java.util.Scanner;

/**
 * Created by andreas on 21.04.14.
 */
public abstract class AbstractController {

    private final Scanner reader;
    private final BibliotecaApp app;
    boolean handleUserInput = true;

    public enum Place {
        MAIN_MENU, BOOK_LIST, MOVIE_LIST, USER_INFORMATION, MY_BOOKS
    }

    public AbstractController(Scanner reader, BibliotecaApp app) {
        this.reader = reader;
        this.app = app;
    }

    protected BibliotecaApp app() {
        return app;
    }

    public void goTo(Place place) {
        handleUserInput = false;
        Controller newController = null;
        switch (place) {
            case MAIN_MENU:
                newController = new MainMenuController(reader(), app());
                break;
            case BOOK_LIST:
                newController = new BookListController(reader(), app());
                break;
            case MOVIE_LIST:
                newController = new MovieListController(reader(), app());
                break;
            case MY_BOOKS:
                newController = new MyBookListController(reader(), app());
                break;
            case USER_INFORMATION:
                newController = new UserInformationController(reader(), app());
                break;
        }
        app.setController(newController);
    }

    protected void handleUserInput() {
        while (handleUserInput) {
            if (reader().hasNext()) {
                String input = readln();
                doHandleUserInput(input);
            }
        }
    }
    
    protected abstract void doHandleUserInput(String input);

    protected Scanner reader() {
        return reader;
    }

    public void println(String content) {
        System.out.println(content);
    }

    public void print(String content) {
        System.out.print(content);
    }

    public String readln() {
        return reader.nextLine();
    }

}
